<?php

/**
 * This is the model class for table "tbl_merchant".
 *
 * The followings are the available columns in table 'tbl_merchant':
 * @property integer $merchant_id
 * @property string $merchant_name
 * @property string $merchant_fullname
 * @property string $merchant_icon
 * @property string $merchant_add
 * @property integer $city_id
 * @property string $merchant_mobile
 *
 * The followings are the available model relations:
 * @property City $city
 * @property Price[] $prices
 */
class Merchant extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_merchant';
	}
    
    /**
    *@return string Merchant's name
    */
    public function getName($merchant_id){
        $merchant=Merchant::model()->findByPk($merchant_id);
        return $merchant['merchant_name'];
    }

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('merchant_name, merchant_icon, city_id', 'required','message'=>REQUIRED_NOT_NULL),
			array('city_id', 'numerical', 'integerOnly'=>true),
			array('merchant_name', 'length', 'max'=>30),
			array('merchant_fullname', 'length', 'max'=>100),
			array('merchant_icon, merchant_add', 'length', 'max'=>255),
			array('merchant_mobile', 'length', 'max'=>11),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('merchant_id, merchant_name, merchant_fullname, merchant_icon, merchant_add, city_id, merchant_mobile', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'city' => array(self::BELONGS_TO, 'City', 'city_id'),
			'prices' => array(self::HAS_MANY, 'Price', 'merchant_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'merchant_id' => 'Mã',
			'merchant_name' => 'Tên đại lý',
			'merchant_fullname' => 'Tên đầy đủ',
			'merchant_icon' => 'Icon',
			'merchant_add' => 'Địa chỉ',
			'city_id' => 'Thành phố',
			'merchant_mobile' => 'Tel',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('merchant_id',$this->merchant_id);
		$criteria->compare('merchant_name',$this->merchant_name,true);
		$criteria->compare('merchant_fullname',$this->merchant_fullname,true);
		//$criteria->compare('merchant_icon',$this->merchant_icon,true);
		$criteria->compare('merchant_add',$this->merchant_add,true);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('merchant_mobile',$this->merchant_mobile,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Merchant the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
